package com.educate.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.educate.commonutils.ResultOne;
import com.educate.serviceedu.client.VodClient;
import com.educate.serviceedu.entity.EduCourse;
import com.educate.serviceedu.entity.EduTeacher;
import com.educate.serviceedu.entity.EduVideo;
import com.educate.serviceedu.entity.vo.CourseInfoVo;
import com.educate.serviceedu.entity.vo.CoursePublishVo;
import com.educate.serviceedu.entity.vo.CourseQuery;
import com.educate.serviceedu.entity.vo.TeacherQuery;
import com.educate.serviceedu.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author educate.wht
 * @since 2022-08-16
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;
    //课程列表基本实现
    @GetMapping("getCourseList")
    public ResultOne getCourseList(){
        List<EduCourse> list = courseService.list(null);
        return ResultOne.ok().data("list",list);

    }
    @ApiOperation("条件查询讲师")
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public ResultOne pageTeacherCondition(@PathVariable long current,
                                          @PathVariable long limit,
                                          @RequestBody(required = false) CourseQuery courseQuery){
        //创建page对象
        Page<EduCourse> pageCourse=new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduCourse> queryWrapper=new QueryWrapper<>();
        //多条件组合查询
        //mybatis学过动态sql
        String title =courseQuery.getTitle();
        String status=courseQuery.getStatus();
        String begin=courseQuery.getBegin();
        String end=courseQuery.getEnd();
        //判断条件值是否为空,如果不为空拼接条件
        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(status) ) {
            queryWrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }
        queryWrapper.orderByDesc("gmt_create");
        //调用方法实现条件查询分页
//        teacherService.page()
//        courseService.page(pageCourse,queryWrapper);
        courseService.page(pageCourse,queryWrapper);
        long total = pageCourse.getTotal();//总记录数目
        List<EduCourse> records = pageCourse.getRecords();//数据list集合
        return ResultOne.ok().data("total",total).data("rows",records);
    }
    //添加课程基本信息
    @PostMapping("addCourseInfo")
    public ResultOne addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回添加之后课程id,为了后面添加大纲使用
        String id=courseService.saveCourseInfo(courseInfoVo);

        return ResultOne.ok().data("courseId",id);
    }

    //根据课程id查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public ResultOne getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo= courseService.getCourseInfo(courseId);
        return ResultOne.ok().data("courseInfoVo",courseInfoVo);
    }
    //修改课程信息
    @PostMapping("updateCourseInfo")
    public ResultOne updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return ResultOne.ok();

    }
    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public ResultOne getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo= courseService.publishCourseInfo(id);
        return ResultOne.ok().data("publishCourse",coursePublishVo);
    }
    //课程最终发布
    @PostMapping("publishCourse/{id}")
    public ResultOne publishCourse(@PathVariable String id){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.saveOrUpdate(eduCourse);
        return ResultOne.ok();
    }
    //删除课程
    @DeleteMapping("deleteCourse/{courseId}")
    public ResultOne deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return ResultOne.ok();
    }

}

